package id.laskarmedia.opnvpn_flutter.handler;


import android.text.TextUtils;

import id.laskarmedia.opnvpn_flutter.OpenVPNEngineer;
import id.laskarmedia.opnvpn_flutter.enums.VPNEventEnum;
import id.laskarmedia.opnvpn_flutter.enums.VPNStage;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;

/**
 * AbstractEventHandler is an abstract class that implements the VPNEventHandler interface.
 */
public abstract class AbstractEventHandler implements VPNEventHandler {

    protected EventChannel.EventSink vpnStageSink;

    protected final static OpenVPNEngineer OPEN_VPN_ENGINEER = new OpenVPNEngineer();

    /**
     * Returns the VPN event type handled by this event handler.
     *
     * @return The VPNEventEnum representing the VPN event type.
     */
    public abstract VPNEventEnum getVPNEvent();

    /**
     * Updates the VPN state.
     *
     * @param stage The VPN state.
     */
    protected void updateState(String stage) {
        if (TextUtils.isEmpty(stage)) {
            stage = VPNStage.idle.getStatus();
        }
        if (vpnStageSink != null) {
            vpnStageSink.success(stage.toLowerCase());
        }
    }

    /**
     * Configures the EventSink.
     *
     * @param eventSink The EventSink object used for sending VPN events.
     */
    @Override
    public void config(EventChannel.EventSink eventSink) {
        if (eventSink != null) {
            this.vpnStageSink = eventSink;
        }
    }
}
